[% setvar title New pragma 'scope' to change Perl's default scoping %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>New pragma 'scope' to change Perl's default scoping</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Nathan Wiger &lt;<a href='mailto:nate@wiger.org'>nate@wiger.org</a>&gt;
  Date: 7 Aug 2000
  Last Modified: 28 Aug 2000
  Mailing List: <a href='mailto:perl6-language-strict@perl.org'>perl6-language-strict@perl.org</a>
  Number: 64
  Version: 2
  Status: Frozen</pre>
<a name='STATUS'></a><h1>STATUS</h1>
<p>This RFC was relatively well-received. While many questioned how useful
<code>use scope 'blocks'</code> could be, overall most people were quite receptive
to the <code>use scope 'subs'</code> concept.</p>
<p>This RFC has been frozen. It is not expected to grow further. Since it
is a pragma it is not a core piece of functionality and could be added
at any time.</p>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Historically, Perl has had the default &quot;everything's global&quot; scope. This
means that you must explicitly define every variable with my, our, or
its absolute package to get better scoping. You can 'use strict' to
force this behavior, but this makes easy things harder, and doesn't fix
the actual problem.</p>
<p>Those who don't learn from history are doomed to repeat it. Let's fix
this.</p>
<a name='MOTIVATORS'></a><h1>MOTIVATORS</h1>
<p>Tons of Impatience, a good amount of Laziness, and a dash of Hubris</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='Overview'></a><h2>Overview</h2>
<p>First off, let's clear up what this RFC does NOT do: it does <b>NOT</b>
change Perl's default scoping. By default, Perl should be scoped
globally, as it always has been and always should continue to be.
Rather, this RFC suggests a pragma that will offer alternative scoping
rules, to make easy things even easier.</p>
<p>In a default Perl script, everything is global. Which means in:</p>
<pre>   $x = 10;
   if (1) {
      unless (undef) {
         $y = 1 if ($x);
      }
   }</pre>
<p>$x and $y are in the same scope. This is good, but also bad. For one
thing, you can hang yourself real easily if you come from a C background
and expect this to keep stuff private:</p>
<pre>   $x = 10;
   sub square {
      ($x) = @_;
      $x *= $x;
   }
   $ans = square($x);
   print &quot;$x squared is $ans\n&quot;;  # &quot;100 squared is 100&quot; ?</pre>
<p>Ooops. What happened? Turns out the sub square()'s $x is the same $x as
the one outside. Bad news, you reset the (global) value of $x. Ooops.</p>
<p>RFC 6 addresses this problem, but in the wrong way. In Perl, you can
'use strict' to force you to predeclare all your variables with my or
our (which you can do anyways if you feel like it). So the above code
becomes:</p>
<pre>   use strict;
   my $x = 10;
   sub square {
      my($x) = @_;
      $x *= $x;
   }
   my $ans = square($x);
   print &quot;$x squared is $ans\n&quot;;</pre>
<p>So, with the addition of <code>use strict</code> and 3 &quot;little&quot; <code>my</code>'s, we've
resolved the ambiguity. Problem is, this is a good amount of work
(especially if you're Lazy *and* Impatient, like I am) for such a stupid
little 5-line script. Plus, it makes the code markedly harder to read.
Setting 'use strict' to the default makes easy things harder - very
UN-Perlish! Eck.</p>
<p>The better solution is to fix the actual *problem* (Perl's scoping), and
provide a method for automatically scoping variables via different
algorithms than Perl's default. As Daniel Chetlin notes in RFC 16
(against strict defaults):</p>
<pre>   Making strict 'vars' default appears to be an attempt to fix
   a hole in the floor by permanently barring access to the room
   in which the hole is, rather than just patching the hole.</pre>
<p>Let's patch the hole.</p>
<a name='Different Scoping Algorithms'></a><h2>Different Scoping Algorithms</h2>
<p>This RFC describes several different types of scoping. They can be used
together, yielding advanced scoping without the mess of <code>my</code>. Each of
these would be specified as:</p>
<pre>   use scope qw(types);</pre>
<p>Where &quot;types&quot; are the types listed below. By default, 'all' is used,
which turns on all scoping rules. As of version 1, there are currently
two proposed types of scopes: <code>subs</code> and <code>blocks</code>.</p>
<p>Note: You can always still use an explicit <code>my</code> or <code>our</code> keyword to
override the <code>scope</code> pragma. The <code>scope</code> pragma only affects variables
that are not explicitly declared. This is important, since it means that
no functionality is reduced by specifying <code>use scope</code>.</p>
<a name='subs'></a><h3>subs</h3>
<p>The <code>subs</code> scoping pragma makes one key change: it automatically scopes
variables within subs as if they were <code>my</code>'ed at the top of the sub.
So, the code:</p>
<pre>   use scope 'subs';

   sub dostuff {
       ($one, $two, $three) = @_
       if ( $two &gt; 5 ) {
          $four = 1;
       } else {
          $five = 2;
          while ( $five &lt; 6 ) {
              $five++;
          }
       }
       return $four || $five;
   }</pre>
<p>Would be internally changed to the same thing as if all of <code>dostuff</code>'s
variables had been declared with <code>my</code> as the first statement in the
sub. This keeps you from clobbering yourself with:</p>
<pre>   ($three, $four, $five) = readvals;
   $six = dostuff($five, $three, $four);</pre>
<p>Without the need to declare all those <code>my</code>'s all over the place.</p>
<p>The exceptions to the <code>subs</code> scoping pragma are BEGIN, END, and any
other special Perl subs that may apply.</p>
<a name='blocks'></a><h3>blocks</h3>
<p>The <code>blocks</code> scoping pragma automatically scopes variables to the
innermost <b>anonymous</b> block. The key work here is anonymous. The main
program itself is seen as the outermost block. So, this code:</p>
<pre>   $x = 10;
   {
      $x = 5;
   }
   if ($x == 10) {
      $x = 25;
   }
   print &quot;$x\n&quot;;</pre>
<p>Would result in &quot;25&quot; being printed out. Here's why:</p>
<pre>   1. The C&lt;$x = 10&gt; is automatically scoped with its own C&lt;my&gt;.

   2. The C&lt;$x = 5&gt; inside the B&lt;anonymous&gt; block is automatically
      scoped with its own C&lt;my&gt;.

   3. The C&lt;$x = 25&gt; code, however, inside the C&lt;if&gt; statement
      is not scoped, since it is not an B&lt;anonymous&gt; block. Since
      there is already an C&lt;$x&gt; in the current block, there is
      no need to rescope it.</pre>
<p>Th <code>blocks</code> scoping pragma also works for do{} blocks, but again not
for BEGIN or END (but those are subs anyways).</p>
<p>To illustrate, here are some examples of how this might be used:</p>
<pre>   1. do {} block

      $val = do {
          $x = 10;
           # ... stuff happens ...
          $y;
      };

   In which case $val = $y.

   2. explicit our() scoping

      $x = 10;
      our $y = 10;
      {
         $x = 5;   # auto-my'ed
         $y += $x; # $y already our'ed above
      }
      print &quot;$y&quot;;  # 15</pre>
<p>Admittedly, the usefulness of this is markedly less than the <code>use scope
'subs'</code> pragma.</p>
<a name='Incompatibility with strict'></a><h2>Incompatibility with strict</h2>
<p>By its very nature, this is incompatible with <code>strict</code>. Specifying both
<code>use strict</code> and &lt;use scope&gt; should generate an error.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Hopefully simple. If you preparsed 'subs' or 'blocks' and just pseudo-
inserted a &quot;my&quot; in front of every variable, you'd be 99% of the way
there.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>This introduces new functionality. No migration path is required.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 16: Keep default Perl free of constraints such as warnings and strict.</p>
<p>RFC 28: Perl should stay Perl.</p>
</div>
